// ActionScript file
package com.wdxc {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.* ; 
    import flash.display.* ;
    import flash.geom.Matrix; 
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.printing.PrintJob;
    import flash.ui.Mouse ; 
    //import flash.utils.Timer; 
    import flash.utils.* ; 
    import com.wdxc.Constant ;
    import fl.controls.* ;
    /** 
    * 图像裁剪
    * @author luqinglong
    */    
    public class ImageCut extends Sprite{ 
           
        //原始图像数据数据
        private var _bitmap:Bitmap ;
        //裁剪区域   
        private var _rect:Sprite ;   
        //裁剪控制菜单
        private var _cutMenu:CutMenu ;  
        //拖动时光标图形
        private var _cursor1:Sprite ;           
        //拉伸时光标图形   
        private var _cursor2:Sprite  ;        
        //记录鼠标信息，  
        private var _mouseInfo:MouseInfo = new MouseInfo() ;
        //鼠标按下时，保存绘制矩形的信息
        private var _tempObj:Object = new Object() ;   
         
        //预览时，临时保存图片数据 
        private var _tempBitmapData:BitmapData ;
          
        private var bg1:Sprite  ;//预览加载背景时用,覆盖菜单
         
        //模糊背景
        private  var _vaguebg:Sprite ;
         
        //裁剪比例         
        private var _cutScale:Number  = 0 ;
           
        //宽和高度裁剪比例  
        public var cutscaleFlag:int = Constant.HORIZONTAL;
            
        public var minPels:Number = 800 * 600;
             
        private var scaleInfo:String = "0";
         
        public function ImageCut() {     
        }         
            
        /** 
         * 创建光标，并使其不可见 。
         */
        private function createCursor():void {
            _cursor1 = new Cursor1() ; 
            _cursor1.x = mouseX ;
            _cursor1.y = mouseY ; 
            _cursor1.visible = false ;
                 
            _cursor2 = new Cursor2() ;
            _cursor2.x = mouseX ;
            _cursor2.y = mouseY ;    
            _cursor2.visible = false ;          
            
            this.addChild(_cursor1);
            this.addChild(_cursor2) ; 
        } 
                  
        /** 
         * 初始化，裁剪控制菜单
         */  
        private function createCutMenu():void {
                
            //显示像素提醒 ；
            Main.SCENE.pelsHint.visible = true ;
            
            _cutMenu = new CutMenu();         
            _cutMenu.x = Main.SCENE.menu_mc.x ;
            _cutMenu.y = Main.SCENE.menu_mc.y ;       
                                  
            this.addChild(_cutMenu);               
            _cutMenu.cutok_btn.addEventListener(MouseEvent.CLICK, saveHint) ;  
            _cutMenu.cutcancle_btn.addEventListener(MouseEvent.CLICK, cutCancle) ;    
            _cutMenu.preview_btn.addEventListener(MouseEvent.CLICK, cutPreview) ;
               
            _cutMenu.rd5_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd6_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd62_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd7_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd8_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd10_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd12_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd16_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd18_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd20_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd24_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd30_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd40_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd0_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
        }          
                           
        /**
         * 创建裁剪框
         */ 
        private function createCutRect():void {
             _rect = new Sprite();   
             _rect.x = _bitmap.x ;   
             _rect.y = _bitmap.y ;
            /***设置裁剪区域边框样式*/           
            _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND);
            _rect.graphics.beginFill(0xCCFF00,0);           
            _rect.graphics.drawRect(0,0,_bitmap.width-20,_bitmap.height-20);     
            _rect.graphics.endFill();                
            this.addChild(_rect);       
        } 
        
        public function set bitmap(bp:Bitmap):void {
            this._bitmap = bp ;
        }    
        public function get bitmap():Bitmap {
            return this._bitmap ;
        }
        
         /****        
         * 开始裁剪。初始化裁剪工具。
         * @param    e     
         */        
        public function startCutPic(e:MouseEvent):void 
        {      
            /**
             * 判断横版照片，竖版照片
             */  
            if ( _bitmap.height > _bitmap.width) {
                //竖版照片  
                this.cutscaleFlag = Constant.VERTICAL ;    
            } else {     
                this.cutscaleFlag = Constant.HORIZONTAL ;
            }
    
            Main._handerState = 5;  
            //cutpicing();
            this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; 
            this.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) ;
            this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler) ;   
            this.createCutMenu() ;    
            this.createCutRect() ;      
            this.createCursor() ;
                        
            //_cutMenu.RadioButtonGroup1.addEventListener(Event.CHANGE, changeHandler); 
        }   
        private function changeHandler(e:Event):void {  
            //RadionButton  = e.target as RadionButton ;
            trace(123);       
        }
          
        /**
         * 比例裁剪
         */
        private function  scaleSelectHandler(e:MouseEvent):void {
            var rb:RadioButton = e.target as RadioButton;
            //为横排照片  
                     
            switch(rb.value) {
                case "5" :      
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 3.5):(3.5 / 5) ;
                    scaleInfo = "5 : 3.5";  
                    this.minPels = 800 * 600 ;
                    break ;    
                case "6" :     
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(3 / 2):(2 / 3) ;
                    scaleInfo = "3 : 2";  
                    this.minPels = 1024 * 768 ;       
                    break ;  
                case "62":  
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ;
                    scaleInfo = "4 : 3";  
                    this.minPels = 1024 * 768 ;
                    break ;     
                case "7" :   
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(7 / 5):(5 / 7) ;
                    scaleInfo = "7 : 5";  
                    this.minPels = 1280 * 960 ;
                    break ;  
                case "8" :     
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ;
                    scaleInfo = "4 : 3";  
                    this.minPels = 1280 * 960 ;  
                    break ;
                case "10":         
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ;
                    scaleInfo = "5 : 4";  
                    this.minPels = 1600 * 1200 ;
                    break ; 
                case "12":     
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(6 / 5):(5 / 6) ;
                    scaleInfo = "6 : 5";  
                    this.minPels = 1600 * 1200 ;
                    break ;
                case "16":      
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "4 : 3";  
                    break ;
                case "18":    
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(3 / 2):(2 / 3) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "3 : 2";  
                    break ;
                case "20":    
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "5 : 4";  
                    break ;
                case "24":    
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(6 / 5):(5 / 6) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "6 : 5";  
                    break ;
                case "30":    
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "5 : 4";  
                    break ;     
                case "40":                 
                    this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ;
                    this.minPels = 2048 * 1953 ;
                    scaleInfo = "4 : 3";  
                    break  
                case "0":                                     
                    this._cutScale = 0;
                    this.minPels = 800 * 600 ;
                    scaleInfo = "0";   
                    break ;   
            }   
            
            this._rect.graphics.clear();
            this._rect.x = _bitmap.x ; 
            this._rect.y = _bitmap.y ;  
            _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND);
            _rect.graphics.beginFill(0xCCFF00, 0);    
                 
            if (this._cutScale == 0) {                  
                          
                _rect.graphics.drawRect(0, 0, _bitmap.width-20,_bitmap.height-20); 
            }else { 
                var w = _bitmap.width  ; 
                var h = w / this._cutScale ;
                   
                /*按比例显示最大尺寸*/
                if (h < _bitmap.height) {
                    _rect.graphics.drawRect(0,0,w,h); 
                }else {             
                    _rect.graphics.drawRect(0,0,_bitmap.height*this._cutScale,_bitmap.height); 
                }  
            }
                
            _rect.graphics.endFill();  
                      
        }        
         
        private function cutCancle(e:MouseEvent):void {
                 
            Main._handerState = 0 ;
            removeCutMenu();
        }    
        
        /**   
         * 保存裁剪应用   
         */       
        private function cutApplication(e:MouseEvent):void {   
               
            // 保存裁剪 导入条 
            Main.SCENE.addLoadingBar();
              
            var server :ImageService = new ImageService();
            server.photoid = Main.SCENE.photoid ;
                  
            var tempx:Number = Math.round(_rect.x - this._bitmap.x);
            var tempy:Number = Math.round(_rect.y - this._bitmap.y) ;
                     
            server.submitCut(tempx, tempy, Math.round(_rect.width), Math.round(_rect.height),this.scaleInfo);
     
            this.removeCutMenu();       
            _cutSaveHint.removeEventListener(MouseEvent.CLICK, removeHintHandler);
            _cutSaveHint.removeEventListener(MouseEvent.CLICK, cutApplication);
            
            this.stage.removeChild(_cutSaveHint);  
            
            Main._handerState = 1 ;    
            
            //得到长边
            var ml = _rect.width > _rect.height?_rect.width:_rect.height ;
                  
            //重新计算中图和大图的比例         
            Main.SCENE.picScale = Main.SCENE.picScale * ml/500 ;
                
            
            
        } 
            
        public var _cutSaveHint:CutSaveHint ;
         
         
        /**
         * 保存时候提示说明
         * @param   e
         */
        private function saveHint(e:MouseEvent):void {
                      
            // = 背景和提示同时存在 
            Main._handerState = 6 ;
            
            Main.SCENE.addVagueBg();
            
                
            _cutSaveHint = new CutSaveHint();
                     
            _cutSaveHint.x = (Constant.LEFT_AREA_WIDTH-_cutSaveHint.width)/2 ;
            _cutSaveHint.y = 200 ;  
                     
            this.stage.addChild(_cutSaveHint);              
                                                                           
            _cutSaveHint.ok_btn.addEventListener(MouseEvent.CLICK, cutApplication);
            _cutSaveHint.cancle1_btn.addEventListener(MouseEvent.CLICK,removeHintHandler);
                 
        
            this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; 
        }       
              
        private function removeHintHandler(e:MouseEvent):void {
                 
            removeHint();
    
        }
        
        public function removeHint():void {
                
            //单击裁剪
            Main._handerState = 5 ;
             
            //删除提示按钮的监听器
            _cutSaveHint.removeEventListener(MouseEvent.CLICK, removeHintHandler);
            _cutSaveHint.removeEventListener(MouseEvent.CLICK, cutApplication);
            this.stage.removeChild(_cutSaveHint);    
                         
                 
            //戍边回复监听移动
            this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ;    
                               
            Main.SCENE.delVagueBg();
        }
                   
            
        /**
         * 裁剪时预览
         * @param   e
         */
        private function cutPreview(e:MouseEvent):void {
                                    
            
            bg1 = new Sprite() ;          
            bg1.graphics.beginFill(0x000000);
            bg1.graphics.drawRect(0, 0, 770, 530);
            bg1.graphics.endFill();
            bg1.alpha = 0;               
            bg1.x = Constant.LEFT_AREA_WIDTH+Constant.MIDDLE_AREA_WIDTH ; 
            this.addChild(bg1);             
        
            //b保存一份副本，预览时候来用。      
            _tempBitmapData  = this._bitmap.bitmapData.clone() ;
            
            
            //存放裁剪出的新图片              
            var cutbitmapdata:BitmapData = new BitmapData(Math.floor(_rect.width),Math.ceil(_rect.height)) ;
         
            //坐标转化，把坐标移到裁剪区域的位置，宽度和高度在cutbitmapdata里指定。 
            var matrix:Matrix = new Matrix(1, 0, 0, 1, -Math.floor(_rect.x-_bitmap.x), -Math.floor(_rect.y-_bitmap.y));   
            cutbitmapdata.draw(this._bitmap.bitmapData, matrix);  
                 
                     
            this._bitmap.bitmapData.dispose() ;            
            this._bitmap.bitmapData = cutbitmapdata ; 
            this._rect.visible = false ;   
            
            Main.SCENE.reset();
            setTimeout(previewCompletehandler, 2000) ;           
        }     
                  
        private function previewCompletehandler():void {
            this._bitmap.bitmapData.dispose();
            this._bitmap.bitmapData = this._tempBitmapData ;
            Main.SCENE.reset();        
            this._rect.visible = true ;     
            this.removeChild(bg1) ;         
        }
          
        /**      
         * 删除裁剪控制菜单 
         */        
        public function removeCutMenu():void {
                 
            //恢复原值 
            Main._handerState = 0 ;
                
            //象素不提醒 
            Main.SCENE.pelsHint.visible = false ;
            
            _cutMenu.cutok_btn.removeEventListener(MouseEvent.MOUSE_DOWN, cutApplication);
            _cutMenu.cutcancle_btn.removeEventListener(MouseEvent.MOUSE_DOWN, cutCancle) ; 
                       
             /*删除裁剪比例监听*/
            _cutMenu.rd5_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd6_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd62_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd7_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd8_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd10_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd12_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd16_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd18_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd20_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd24_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd30_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd40_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            _cutMenu.rd0_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ;
            this._cutScale = 0 ;    
            
    
            this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) ;
            this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler) ;
            this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; 
                 
            this.removeChild(_cutMenu) ;
            this.removeChild(_rect) ;    
            this.removeChild(_cursor1); 
            this.removeChild(_cursor2) ;
        }
          
         
        /**  
         * 鼠标按下处理
         * @param   e
         */
        private function mouseDownHandler(e:MouseEvent):void { 
            
            /*保存按下前状态**/  
            this._tempObj.width = _rect.width-Constant.BORDER ;
            this._tempObj.height = _rect.height-Constant.BORDER ;
            this._tempObj.x = _rect.x ;       
            this._tempObj.y = _rect .y ;        
             
            /*鼠标为按下状态*/
            this._mouseInfo.state = Constant.MOUSE_DOWN ;
        }     
        private function mouseUpHandler(e:MouseEvent):void {
            _rect.stopDrag();     
            /*状态设置为弹起状态**/
            this._mouseInfo.state = Constant.MOUSE_UP ;
        }         
        
        /**
         * 鼠标移动
         * @param   e       
         */
        private function mouseMoveHandler(e:MouseEvent):void {
            Mouse.hide() ; 
            this._cursor1.visible = false ;
            this._cursor2.visible = false ; 
             
            /*拖动左边界*/
            if (_mouseInfo.isStartLeftDrag()) {
                this.resetCursorPosition(Constant.CURSOR_HORIZONTAL) ;
                this.repaint(Constant.LEFT);
            } 
            /*拖动右边界*/
            else if(_mouseInfo.isStartRightDrag()) { 
                this.resetCursorPosition(Constant.CURSOR_HORIZONTAL); 
                this.repaint(Constant.RIGHT);     
            } 
            /*拖动上边界*/
            else if(_mouseInfo.isStartUpDrag()) { 
                this.resetCursorPosition(Constant.CURSOR_VERTICAL); 
                this.repaint(Constant.UP);     
            } 
            /*拖动下边界*/    
            else if(_mouseInfo.isStartDownDrag()) { 
                this.resetCursorPosition(Constant.CURSOR_VERTICAL); 
                this.repaint(Constant.DOWN);      
            } 
            /*拖动整个区域*/ 
            else if (_mouseInfo.isStartMiddleDrag()) {
                this.resetCursorPosition(Constant.CURSOR_MOVE); 
                this.dragHandler();   
            } 
            /*拖动左上角*/
            else if (_mouseInfo.isStratLeftUPDrag()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT) ;
                this.repaint(Constant.LEFT_UP) ;
            } 
            /*拖动左下角*/
            else if (_mouseInfo.isStratLeftDownDrag()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT) ;
                this.repaint(Constant.LEFT_DOWN) ;
            }
            /*拖动右上角*/
            else if (_mouseInfo.isStratRightUPDrag()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT) ;
                this.repaint(Constant.RIGHT_UP) ;
            }           
            /*拖动右下角*/
            else if (_mouseInfo.isStratRightDownDrag()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT) ;
                this.repaint(Constant.RIGHT_DOWN) ;
            }            
            /*鼠标在左上角**/
            else if (isLeftUpAngle()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT);
                this._mouseInfo.position = Constant.LEFT_UP ; 
                 
            }
            /*鼠标在左下角**/
            else if (isLeftDownAngle()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT);
                this._mouseInfo.position = Constant.LEFT_DOWN ; 
                 
            }
            /*鼠标在右上角**/
            else if (isRightUpAngle()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT);
                this._mouseInfo.position = Constant.RIGHT_UP ; 
                 
            }    
            /*鼠标在右下角**/ 
            else if (isRightDownAngle()) {
                this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT);
                this._mouseInfo.position = Constant.RIGHT_DOWN ;     
                 
            }
                         
            /*鼠标在左边界**/
            else if (isLeftEdge()) {  
                this.resetCursorPosition(Constant.CURSOR_HORIZONTAL);      
                this._mouseInfo.position = Constant.LEFT ;
         
            }       
            /*鼠标在右边界**/ 
            else if (isRightEdge()) {        
                this.resetCursorPosition(Constant.CURSOR_HORIZONTAL);    
                this._mouseInfo.position = Constant.RIGHT ;   
            }  
            /*鼠标在上边界**/ 
            else if (isUpEdge()) {   
                this.resetCursorPosition(Constant.CURSOR_VERTICAL);   
                this._mouseInfo.position = Constant.UP ; 
            }           
            /*鼠标在下边界**/ 
            else if (isDownEdge()) {  
                this.resetCursorPosition(Constant.CURSOR_VERTICAL);   
                this._mouseInfo.position = Constant.DOWN ; 
            } 
            /* 鼠标在裁剪区域里。*/
            else if (this.isInCutRect()) { 
                this.resetCursorPosition(Constant.CURSOR_MOVE); 
                this._mouseInfo.position = Constant.MIDDLE ;
            }    
            else {          
                Mouse.show () ;
            }       
            e.updateAfterEvent();
            
              
            /*象素提示**/
            this.pelsHint();        
            
        }   
        
        private function pelsHint():void {                           
            var p = _rect.width * _rect.height * Main.SCENE.picScale * Main.SCENE.picScale ;
            p = Math.round(p);           
            if ( p < this.minPels) {       
                Main.SCENE.setPelsHint(p,"像素太低，继续裁剪将会影响照片的冲印质量。");    
            }else {                 
                Main.SCENE.setPelsHint(p,""); 
            }
              
        }
        
         /**    
          * 重绘裁剪区域 。
          * @param  state 
          */
        private function repaint(state:uint):void {  
            this._rect.graphics.clear() ;               
            _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND);
            _rect.graphics.beginFill(0xCCFF00, 0);      
            //var h:Number = 0 ;
            //var w:Number = 0 ;        
            switch(state) {        
                case Constant.LEFT:       
                    this.leftDragHandler() ;
                    break;  
                case Constant.RIGHT:      
                     this.rightDragHandler();
                    break ;   
                case Constant.UP:  
                     this.upDragHandler();
                     break ;                    
                case Constant.DOWN:  
                    /*下端拖动*/
                    downDragHandler();
                    break;     
                case Constant.LEFT_UP:  
                    this.leftUpDragHandler() ;            
                    break;       
                case Constant.LEFT_DOWN:  
                    /*左下角拖动*/  
                    this.leftDownDragHandler();
                    break;       
                case Constant.RIGHT_UP:   
                    /*右上角拖动*/  
                    this.rightUpDragHandler();
                    break;            
                case Constant.RIGHT_DOWN:    
                     /*右下角拖动*/      
                    this.rightDownDragHandler();
                    break;                      
             }        
            _rect.graphics.endFill();   
        }  
          
        /*左端拖动处理*/     
        private function leftDragHandler():void {
            var w = 0 ;
            var h = 0; 
            if(this._cutScale!=0) {  
                      
                _rect.x = mouseX < _bitmap.x?_bitmap.x:mouseX ;       
                _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ;  
                w = _tempObj.width + _tempObj.x - _rect.x ;  
                h = w / this._cutScale ;
                          
                var h2 = this._bitmap.height+this._bitmap.y - this._tempObj.y ;   
                if(h>=h2){                                         
                    _rect.x = this._tempObj.x + this._tempObj.width -h2*this._cutScale;
                    w = _tempObj.width + _tempObj.x - _rect.x ;  
                    _rect.graphics.drawRect(0, 0, w, h2 );         
                }     
                else {
                    _rect.graphics.drawRect(0, 0, w , h );   
                }
  
            }       
            else {
                 /*左端拖动*/          
                _rect.x = mouseX < _bitmap.x?_bitmap.x:mouseX ;       
                _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ;   
                _rect.graphics.drawRect(0, 0, _tempObj.width + _tempObj.x - _rect.x, _tempObj.height);   
             
            }
        }
          
        private function rightDragHandler():void {
            var w:Number = 0 ;
            var h:Number = 0 ;  
            if (this._cutScale != 0) {
                /*右端拖动*/       
                w = mouseX > (w = (_bitmap.x + _bitmap.width))?w:mouseX ;
                w = w <= this._tempObj.x?_tempObj.x:w ;   
                 
                //宽度
                w =  w -this._tempObj.x ;    
                h = w/ this._cutScale ;
                _rect.y = _tempObj.height + _tempObj.y - h;
                
                if (_rect.y < _bitmap.y) {                       
                    h = _tempObj.height + _tempObj.y -_bitmap.y ;
                    _rect.y = _bitmap.y ;            
                    w =  h * this._cutScale ; 
                    //w = w - this._tempObj.x ;          
                    
                    //trace("h=="+h); 
                    
                }
                //trace("_rect.y=="+_rect.y+" bitmap.y=="+_bitmap.y);
                     
                _rect.graphics.drawRect(0, 0, w,h);   
                  
            } 
            else { 
                /*右端拖动,任意裁剪*/   
                w = mouseX > (w = (_bitmap.x + _bitmap.width))?w:mouseX ;
                w = w <= this._tempObj.x?_tempObj.x:w ;   
                _rect.graphics.drawRect(0, 0, w -this._tempObj.x, _tempObj.height);  
            }
                    
        }
          
        /**
         * 上端拖动处理
         */   
        private function upDragHandler():void {
            
            if (this._cutScale != 0) {
                /*按一定比例裁剪**/ 
                _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY;      
                _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ;  
                    
                var h = _tempObj.height + _tempObj.y-_rect.y ;
                var w = h * this._cutScale ;        
                       
                _rect.x = _tempObj.x + _tempObj.width - w ;
                 
                /**
                 * 超过最左边
                 */
                if (_rect.x < _bitmap.x) {  
                    w = _tempObj.x + _tempObj.width - _bitmap.x ;
                    _rect.x = _bitmap.x ;   
                    
                    h = w / this._cutScale ;
                             
                    _rect.y = _tempObj.y + _tempObj.height - h ;
                }
                  
                _rect.graphics.drawRect(0, 0,w,h);  
    
            }else {
                /*任意尺寸裁剪**/
                _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY;      
                _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ;  
                _rect.graphics.drawRect(0, 0, _tempObj.width,_tempObj.height+_tempObj.y-_rect.y);  
            }
        }
             
        
        /**
         * 下端拖动处理
         */ 
        private function downDragHandler():void {
            var h = 0 ;
            var w = 0;
            if (this._cutScale != 0) {
                h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ;
                h = h <= this._tempObj.y?_tempObj.y:h ;  
                //高度
                h = h - _tempObj.y ;
                w = h * this._cutScale ;  
                                    
                if ((w + _tempObj.x) >= (_bitmap.width+_bitmap.x)) {
                    w = _bitmap.x + _bitmap.width - _rect.x ;
                    h = w / this._cutScale ;
                         
                }   
                _rect.graphics.drawRect(0, 0, w, h);
            }else {
                h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ;
                h = h <= this._tempObj.y?_tempObj.y:h ;  
                _rect.graphics.drawRect(0, 0, _tempObj.width, h-_tempObj.y);
            }
        }
          
        /** 
         * 左上拖动处理
         */
        private function leftUpDragHandler():void {
            
            if (this._cutScale != 0) {
                this.upDragHandler();
            }else {
                /*左上角拖动*/    
                _rect.x = mouseX <= _bitmap.x?_bitmap.x:mouseX ;   
                _rect.y = (mouseY <= _bitmap.y)?_bitmap.y:mouseY;  
                
                var w = _tempObj.width + _tempObj.x ;
                var h = _tempObj.height + _tempObj.y ;
                _rect.x = _rect.x >= (w)?(w):_rect.x ; 
                _rect.y = _rect.y >=(h)?(h): _rect.y ; 
                _rect.graphics.drawRect(0, 0, w-_rect.x,h-_rect.y);   
            } 
        }
              
        /**
         * 左下按比例拖动处理
         */
        private function leftDownDragHandler():void {
            if (this._cutScale != 0) {
                this.leftDragHandler();
            }else { 
                _rect.x = mouseX <= _bitmap.x?_bitmap.x:mouseX ;   
                 _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ;     
                var h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ;
                h = h <= this._tempObj.y?_tempObj.y:h ;    
                _rect.graphics.drawRect(0, 0, _tempObj.width + _tempObj.x - _rect.x,h-_tempObj.y);   
            } 
        }   
        
        private function rightDownDragHandler():void {
            if (this._cutScale != 0) {
                this.downDragHandler();   
            }else {     
                var w = mouseY > (w = (_bitmap.y + _bitmap.height))?w:mouseY ;
                w = w <= this._tempObj.y?_tempObj.y:w ;   
                var h = mouseX > (h = (_bitmap.x + _bitmap.width))?h:mouseX ;
                h = h <= this._tempObj.x?_tempObj.x:h    
                _rect.graphics.drawRect(0, 0, h -this._tempObj.x, w-_tempObj.y);         
            }      
        }  
           
        private function rightUpDragHandler():void {
            if (this._cutScale != 0) {
                this.rightDragHandler();   
            }else {   
                _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY;      
                _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ;     
                 var h = mouseX > (h = (_bitmap.x + _bitmap.width))?h:mouseX ;
                 h = h <= this._tempObj.x?_tempObj.x:h   
                _rect.graphics.drawRect(0, 0, h -this._tempObj.x,_tempObj.height+_tempObj.y-_rect.y);   
             
            }     
        }
           
        /**     
         * 重新设置光标的位置,形状 。
         * @param   p     
         */
        private function resetCursorPosition(cursor:uint):void {
            /*移动状态*/
            if (cursor == Constant.CURSOR_MOVE) {
                this._cursor1.x = mouseX ; 
                this._cursor1.y = mouseY ; 
                this._cursor1.visible = true ;  
                return ;   
            }
            this._cursor2.x = mouseX ;
            this._cursor2.y = mouseY ; 
            this._cursor2.visible = true ;        
            switch(cursor) {
                case Constant.CURSOR_HORIZONTAL: 
                    _cursor2.rotation = 0 ;
                    break ; 
                case Constant.CURSOR_VERTICAL:   
                    _cursor2.rotation = 90 ;
                    break ;
                case Constant.CURSOR_DIAGONAL_LEFT:
                    _cursor2.rotation = 45 ;
                    break ; 
                case Constant.CURSOR_DIAGONAL_RIGHT:   
                    _cursor2.rotation = 135 ;
                    break ;
            }
        } 
        
        /** 
         * 拖动 裁剪区域
         */ 
        private function dragHandler():void {
            //裁剪区域移动范围，不能超过原图区域。 
            var _rectangle:Rectangle = new Rectangle();
            _rectangle.x = _bitmap.x ;
            _rectangle.y = _bitmap.y ; 
            _rectangle.width = this._bitmap.width-_rect.width ; 
            _rectangle.height = this._bitmap.height-_rect.height ;       
            _rect.startDrag(false, _rectangle);                     
        }
        
        /**   
         * 检查光标是否在左边界上  
         * @return boolean
         */                 
        private function isLeftEdge():Boolean {
            
            /**检查左边界*/          
            if (mouseX>=(this._rect.x - Constant.ACTIVERANGE) && 
                mouseX<=(this._rect.x + Constant.ACTIVERANGE) &&   
                mouseY>this._rect.y &&    
                mouseY<(this._rect.y +this._rect.height)) {
                    return true ;          
            }              
            return false ;   
        } 
         
        /**
         * 检查光标是否在右边界上
         */  
        private function isRightEdge():Boolean {
            if (mouseX>=(this._rect.x + this._rect.width - Constant.ACTIVERANGE) && 
                mouseX<=(this._rect.x+this._rect.width + Constant.ACTIVERANGE) &
                mouseY>this._rect.y&& 
                mouseY<(this._rect.y+this._rect.height)) {
                    return true ;             
            }      
            return false ;
        }
         
        /** 
         * 检查光标是否在上边界上 。
         * @return boolean .
         */  
        private function isUpEdge():Boolean { 
            if (mouseX > this._rect.x && mouseX < (this._rect.x + _rect.width) &
                mouseY > (_rect.y - Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+Constant.ACTIVERANGE)) {
                return true ;           
            } 
            return false ;
        }
        /** 
         * 检查光标是否在下边界上 。
         * @return boolean .
         */ 
        private function isDownEdge():Boolean { 
            if (mouseX > this._rect.x && mouseX < (this._rect.x + _rect.width) &
                mouseY > (_rect.y + _rect.height - Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) {
                return true ;
            } 
            return false ;
        }
        /**   
         * 检查光标是否在左上角 。
         * @return boolean .   
         */ 
        private function isLeftUpAngle():Boolean { 
            if (mouseX > (this._rect.x-Constant.ACTIVERANGE) && mouseX < (this._rect.x +Constant.ACTIVERANGE) &
                mouseY > (_rect.y  - Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+Constant.ACTIVERANGE)) {        
                return true ;
            }  
            return false ;
        }
        /**    
         * 检查光标是否在左下角 。
         * @return boolean .   
         */ 
        private function isLeftDownAngle():Boolean { 
            if (mouseX > (this._rect.x-Constant.ACTIVERANGE) && mouseX < (this._rect.x +Constant.ACTIVERANGE) &
                mouseY > (_rect.y+_rect.height  - Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) {        
                return true ; 
            }  
            return false ;
        }   
        /**    
         * 检查光标是否在右上角 。
         * @return boolean .   
         */       
        private function isRightUpAngle():Boolean { 
            if (mouseX > (this._rect.x + _rect.width - Constant.ACTIVERANGE) && 
                mouseX < (this._rect.x+_rect.width +Constant.ACTIVERANGE) &
                mouseY > (_rect.y- Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+Constant.ACTIVERANGE)) {        
                return true ; 
            }      
            return false ;
        }
        /**     
         * 检查光标是否在右下角 。
         * @return boolean .   
         */        
        private function isRightDownAngle():Boolean { 
            if (mouseX > (this._rect.x + _rect.width - Constant.ACTIVERANGE) && 
                mouseX < (this._rect.x+_rect.width +Constant.ACTIVERANGE) &
                mouseY > (_rect.y+_rect.height- Constant.ACTIVERANGE) && 
                mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) {        
                return true ;    
            }      
            return false ;
        }       
          
        /**  
         * 判断光标标是否在裁剪区域里面
         * @return boolean 
         */          
        private function isInCutRect():Boolean {   
            if (mouseX>(this._rect.x + Constant.ACTIVERANGE) && 
                mouseX<(this._rect.x + this._rect.width - Constant.ACTIVERANGE) &
                mouseY>(this._rect.y + Constant.ACTIVERANGE) &&    
                mouseY<(this._rect.y + this._rect.height -Constant.ACTIVERANGE)) {
                    return true ;
            }   
            return false ;  
        }
    }
}